package com.easipass.exangeservice.ucx.osgi;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import com.easipass.exangeservice.monitor.FileMsgMonitor;
import com.easipass.exangeservice.ucx.UcxService;
import com.easipass.unifiedconfig.ConfigManager;

/**
 * 
 * @version $Id$
 * @author itea
 * @date 2010-3-5
 */
public class UcxServiceActivator implements BundleActivator {

	FileMsgMonitor msgMonitor;
	
	ServiceReference configManagerRef;
	
	public void start(BundleContext context) throws Exception {
		String path = context.getProperty("com.easipass.exangeservice.ucx.monitorpath");
		if(path == null || path.trim().length() == 0)
			throw new Exception("Cannot find property: com.easipass.exangeservice.ucx.monitorpath");
		
		configManagerRef = context.getServiceReference(ConfigManager.class.getCanonicalName());
		if(configManagerRef == null)
			throw new Exception("Cannot find ConfigManager service.");
		ConfigManager cm = (ConfigManager)context.getService(configManagerRef);
		
		msgMonitor = new FileMsgMonitor(path);
		UcxService ucxListener = new UcxService();
		ucxListener.setConfigManager(cm);
		msgMonitor.setMsgListener(ucxListener);
		msgMonitor.setPeriod(10000);
		msgMonitor.start();
	}

	public void stop(BundleContext context) throws Exception {
		if(msgMonitor != null) msgMonitor.stop();
		context.ungetService(configManagerRef);
	}

}
